/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (theLicense");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-20
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "CardViewStackNode.h"
#include "NativeNodeApi.h"
#include <ace/xcomponent/native_interface_xcomponent.h>
#include <memory>

namespace rnoh {
float g_maxElevation{10.0};
float g_cardElevation{0.0};


CardViewStackNode::CardViewStackNode()
    : ArkUINode(NativeNodeApi::getInstance()->createNode(ArkUI_NodeType::ARKUI_NODE_STACK)) {}

void CardViewStackNode::insertChild(ArkUINode &child, std::size_t index)
{
    maybeThrow(NativeNodeApi::getInstance()->addChild(m_nodeHandle, child.getArkUINodeHandle()));
    }

    void CardViewStackNode::removeChild(ArkUINode &child)
    {
        maybeThrow(NativeNodeApi::getInstance()->removeChild(m_nodeHandle, child.getArkUINodeHandle()));
    }
    
    CardViewStackNode &CardViewStackNode::setBackgroundColor(int const &backgroundColor)
    {
        ArkUI_NumberValue indexValue[] = {{.i32 = backgroundColor}};
        ArkUI_AttributeItem indexItem = {indexValue, sizeof(indexValue) / sizeof(ArkUI_NumberValue)};
        maybeThrow(NativeNodeApi::getInstance()->setAttribute(m_nodeHandle, NODE_BACKGROUND_COLOR, &indexItem));
        return *this;
    }
    CardViewStackNode &CardViewStackNode::setCornerRadius(const float &radius)
    {
        ArkUI_NumberValue indexValue[] = {{.f32 = radius}};
        ArkUI_AttributeItem indexItem = {indexValue, sizeof(indexValue) / sizeof(ArkUI_NumberValue)};
        maybeThrow(NativeNodeApi::getInstance()->setAttribute(m_nodeHandle, NODE_BORDER_RADIUS, &indexItem));
        return *this;
    }
    CardViewStackNode &CardViewStackNode::setCardElevation(const float &elevation)
    {
        if (elevation > g_maxElevation) {
            g_cardElevation = g_maxElevation;
        } else {
            g_cardElevation = elevation;
        }
        ArkUI_NumberValue indexValue[] = {15,
                                          0,
                                          {.f32 = g_cardElevation},
                                          {.f32 = g_cardElevation},
                                          ARKUI_SHADOW_TYPE_COLOR,
                                          static_cast<float>(0XFF7080BB),
                                          0};
        ArkUI_AttributeItem indexItem = {indexValue, sizeof(indexValue) / sizeof(ArkUI_NumberValue)};
        maybeThrow(NativeNodeApi::getInstance()->setAttribute(m_nodeHandle, NODE_CUSTOM_SHADOW, &indexItem));
        return *this;
    }
    CardViewStackNode &CardViewStackNode::setCardMaxElevation(const float &maxElevation)
    {
        g_maxElevation = maxElevation;
        return *this;
    }
} // namespace rnoh